package sdc.media.motorbike.View;

import sdc.media.motorbike.MotorBike;
import sdc.media.motorbike.Controller.Assets;
import sdc.media.motorbike.Controller.Setting;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

public class HighScoreScreen extends BaseScreen {

	// Background and button, etc...
	private Image imgBack;
	private Image buttonExit;
	
	private boolean isExitClick;
	
	private float sizeX = Gdx.graphics.getWidth() / 480.0f;
	private float sizeY = Gdx.graphics.getHeight() / 800.0f;

	@Override
	public void render(float delta) {
		super.render(delta);
		batch.begin();
		batch.draw(Assets.highScore, 100 * sizeX, 220 * sizeY, Assets.highScore.getRegionWidth() * sizeX, Assets.highScore.getRegionHeight() * sizeY);
		//Assets.font_1.setScale(sizeX, sizeY);
		Assets.font_2.setScale(sizeX, sizeY);
		//Assets.font_1.draw(batch, "TOP SCORE", 180 * sizeX, 700 * sizeY);
		//String names[] = Setting.names;
		//int score[] = Setting.scores;
		for (int i = 0; i < Setting.savedata.getPlayerName().length; i++) {
			Assets.font_2.draw(batch, Setting.savedata.getPlayerName()[i], 130 * sizeX, (500 - 38 * i) * sizeY);
			Assets.font_2.draw(batch, String.valueOf(Setting.savedata.getPlayerScore()[i]), 300 * sizeX, (500 - 38 * i) * sizeY);
			//System.out.println(names[i] + ": "+score[i]);
		}
		
		if(isExitClick)
			batch.draw(Assets.buttonExit_click, 165 * sizeX, 200 * sizeY, Assets.buttonExit_click.getRegionWidth() * sizeX, Assets.buttonExit_click.getRegionHeight() * sizeY);
		else
			batch.draw(Assets.buttonExit, 165 * sizeX, 200 * sizeY, Assets.buttonExit.getRegionWidth() * sizeX, Assets.buttonExit.getRegionHeight() * sizeY);
		
		batch.end();
	}

	@Override
	public void resize(int width, int height) {
		super.resize(width, height);
	}

	@Override
	public void show() {
		super.show();
		
		// Constructor
		imgBack = new Image(Assets.backgroundMenu);
		imgBack.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		stage.addActor(imgBack);
		
		buttonExit = new Image();
		buttonExit.setSize(Assets.buttonExit.getRegionWidth() * sizeX, Assets.buttonExit.getRegionHeight() * sizeY);
		buttonExit.setPosition(165 * sizeX, 200 * sizeY);
		stage.addActor(buttonExit);

		if (Setting.soundEnabled) {
			// Play music and sound
			Assets.backgroundPlayMusic.play();
		} else {
			//Pause music and sound
			Assets.backgroundPlayMusic.pause();
		}

		// Button Event...
		buttonExit.addListener(new ClickListener() {
			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				isExitClick = false;
				MotorBike.getManagerScreen().createScreen(ManagerScreen.SCREEN_MENU);
				super.touchUp(event, x, y, pointer, button);
			}

			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				Assets.playSound(Assets.buttonClick);
				isExitClick = true;
				return super.touchDown(event, x, y, pointer, button);
			}
		});
		
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		super.hide();
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		super.pause();
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		super.resume();
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		super.dispose();
	}

}
